package com.taiyi.renaissancemall.ui.mine.wallet.transfer;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.RegexUtil;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.GoTakeMoneyBean;
import com.taiyi.renaissancemall.bean.PersonInfoBean;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.ui.mine.wallet.MyWalletActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TransferActivity extends BaseActivity<TransferView, TransferPresent> implements TransferView {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.transferAmount)
    TextView transferAmount;
    @BindView(R.id.maximumTransferTv)
    TextView maximumTransferTv;
    @BindView(R.id.transferAmountEt)
    EditText transferAmountEt;
    @BindView(R.id.phoneNumber)
    TextView phoneNumber;
    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.forbidTransferTv)
    TextView forbidTransferTv;
    @BindView(R.id.transferTv)
    TextView transferTv;
    @BindView(R.id.top)
    ConstraintLayout top;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_transfer;
    }

    @Override
    public TransferPresent initPresenter() {
        return new TransferPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.transfer));
        RegexUtil.pointEdit(transferAmountEt, false);
        phoneNumberEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 11 && RegexUtil.checkPhone(s.toString())) {
                    showProgress();
                    presenter.getPersonInfo(s.toString());
                }
            }
        });
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {
        presenter.getTransferDetail();
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void getTransferSuccess(GoTakeMoneyBean goTakeMoneyBean) {
        String maxTv = getString(R.string.maximumTransfer) + ":" + goTakeMoneyBean.user.liquannumti;
        maximumTransferTv.setText(maxTv);
    }

    @Override
    public void getTransferFailure(String msg) {
        forbidTransferTv.setVisibility(View.VISIBLE);
        forbidTransferTv.setText(msg);
    }

    @Override
    public void toTransferSuccess(String msg_cn) {
        forbidTransferTv.setVisibility(View.VISIBLE);
        forbidTransferTv.setText(msg_cn);
        Intent intent = new Intent(this, MyWalletActivity.class);
        intent.putExtra("toWithdrawSuccess", 1);
        startActivity(intent);
        finish();
    }

    @Override
    public void toTransferFailure(String msg) {
        forbidTransferTv.setVisibility(View.VISIBLE);
        forbidTransferTv.setText(msg);
    }

    @Override
    public void getPersonInfoSuccess(PersonInfoBean personInfoBean) {
        hideProgress();
        String hide_tel = RegexUtil.phoneNoHide(personInfoBean.getTel());
        if (TextUtils.isEmpty(personInfoBean.getRealname())
                || personInfoBean.getRealname() == null
                || "null".equals(personInfoBean.getRealname())) {
            nameTv.setText(hide_tel);
        } else {
            nameTv.setText(personInfoBean.getRealname());
        }
    }

    @Override
    public void getPersonInfoFailure(String msg) {
        hideProgress();
        if (msg.contains("操作失败")) {
            ToastUtils.showShortToast("该用户不存在或是未注册用户");
        } else {
            ToastUtils.showShortToast(msg);
        }
    }

    @OnClick({R.id.backIv, R.id.transferTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.transferTv:
                String tMoney = transferAmountEt.getText().toString().trim();
                String tPhone = phoneNumberEt.getText().toString().trim();
                if (!TextUtils.isEmpty(tMoney) && !TextUtils.isEmpty(tPhone)) {
                    if (RegexUtil.isMobileNO(tPhone)) {
                        presenter.toTransfer(tPhone, tMoney);
                    } else {
                        ToastUtils.showLongToast("手机号格式不正确");
                    }
                } else {
                    ToastUtils.showLongToast("不能为空");
                }
                break;
        }
    }
}
